/*
 * @Author: chenxuan
 * @Date: 2021-12-16 23:28:40
 * @LastEditTime: 2023-09-05 15:27:20
 * @LastEditors: chenxuan
 */
const path = require('path')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const HTMLWebpackPlugin = require('html-webpack-plugin')
const CopyPlugin = require('copy-webpack-plugin')
const webpack = require('webpack')
const envURl = require('./env.js')
const { ModuleFederationPlugin } = require('webpack').container
module.exports = {
  devtool: 'eval-source-map',
  entry: {
    main: './src/index.js',
    app1: './public-path'
  },
  // mode: "development",
  output: {
    // publicPath:'/', 此字段不能配置
    path: path.resolve(__dirname, 'dist'),
    filename: '[name].js'
  },
  // webpack5自动更新需要配置此项，此项配置后会对模块联邦产生影响
  optimization: {
    runtimeChunk: 'single'
  },
  devServer: {
    port: 3001,
    open: true,
    hot: true,
    watchFiles: ['./public/index.html'],
    proxy: {
      '/apiDbunit': {
        timeout: 30000, // 请求超时时间
        target: 'http://10.64.15.86:8083',
        // target: "http://10.110.162.13:8082",
        changeOrigin: true,
        pathRewrite: {
          '^/apiDbunit': ''
        }
      }
    },
    historyApiFallback: true
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      },
      {
        test: /\.vue$/,
        loader: 'vue-loader'
      },
      {
        test: /\.css$/,
        use: [
          'vue-style-loader',
          'css-loader'
        ]
      },
      {
        test: /\.less$/,
        use: [
          'style-loader',
          'css-loader',
          'less-loader'
        ]
      },
      {
        test: /\.scss$/,
        use: [
          { loader: 'style-loader'},
          { loader: 'css-loader'},
          { loader: 'sass-loader'}
        ]
      },
      // {
      //   test: /\.(eot|svg|ttf|woff|woff2)$/,
      //   loader: 'file-loader'
      // },
      {
        test: /\.(jpg|png|gif|svg)$/,
        loader: 'url-loader',
        options: {
          limit: 12 * 1024,
          esModule: false,
          name: '[hash:10].[ext]'
        },
        type: 'javascript/auto'
      },
      {
        test: /\.(eot|svg|ttf|woff|woff2)(\?\S*)?$/,
        loader: 'file-loader'
      }
    ]
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src/')
    },
    extensions: ['.js', '.vue', '.json', '.index.vue', '.index.js']
  },
  plugins: [
    // 请确保引入这个插件！
    new VueLoaderPlugin(),
    new HTMLWebpackPlugin({
      template: path.resolve(__dirname, './public/index.html')
    }),
    new CopyPlugin({
      patterns: [
        { from: path.resolve(__dirname, 'static'), to: path.resolve(__dirname, 'dist/static') }
      ]
    }),
    new ModuleFederationPlugin({
      // 提供给其他服务加载的文件 可自定义
      filename: 'file-unit.js',
      // 唯一ID，用于标记当前服务
      name: 'unit',
      library: {type: 'var', name: 'unit'},
      // 表示导出的模块，只有在此申明的模块才可以作为远程依赖被使用。
      // 用来声明会提供哪些资源供远程使用
      exposes: [
        './public-path',
        {'./Test': './src/views/test.vue'},
        {'Test': './src/views/test.vue'}
      ],
      shared: ['qs', 'element-ui']
    }),
    new webpack.DefinePlugin({
      BASEURL_BASE: JSON.stringify(envURl),
      BASEURL_BASE1: 'xxxx',
      RESOK: '000000'
    })
  ]
}
